package com.lonely.inventory.service.impl;

import cn.hutool.core.lang.Tuple;
import com.lonely.common.utils.SecurityUtils;
import com.lonely.inventory.domain.entity.SysSurplusInventory;
import com.lonely.inventory.domain.entity.SysSurplusInventorySizeInfo;
import com.lonely.inventory.service.ISurplusInventoryLogService;
import com.lonely.system.domain.entity.SysBusinessLog;
import com.lonely.system.engine.log.GetLogTimeModuleEnum;
import com.lonely.system.service.IBusinessLogService;
import com.lonely.system.service.ISysFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : 黄志标
 * @date: 2025/9/19 10:03
 * @description: 尾货库存日志服务实现类
 */
@Service
public class SurplusInventoryLogServiceImpl implements ISurplusInventoryLogService {

    @Autowired
    private IBusinessLogService businessLogService;

    @Autowired
    private ISysFactoryService factoryService;

    /**
     * 新增尾货库存接口的日志
     *
     * @param sysSurplusInventory
     * @param sysSurplusInventorySizeInfos
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSurplusInventoryLog(SysSurplusInventory sysSurplusInventory, List<SysSurplusInventorySizeInfo> sysSurplusInventorySizeInfos) {
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        sysBusinessLog.setRefId(sysSurplusInventory.getId());
        sysBusinessLog.setModuleName(GetLogTimeModuleEnum.SURPLUS_INVENTORY.name());
        sysBusinessLog.setLogTitle("【新增】");
        sysBusinessLog.setLogBody(buildAddSurplusInventoryLogBody(sysSurplusInventory, sysSurplusInventorySizeInfos));
        sysBusinessLog.setCreateBy(SecurityUtils.getUserId());
        sysBusinessLog.setUpdateBy(SecurityUtils.getUserId());
        this.businessLogService.save(sysBusinessLog);
    }

    /**
     * 更新尾货库存接口的日志
     *
     * @param surplusInventory
     * @param comparisonData
     */
    @Override
    public void editSurplusInventoryLog(SysSurplusInventory surplusInventory, Map<String, Tuple> comparisonData) {
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        sysBusinessLog.setRefId(surplusInventory.getId());
        sysBusinessLog.setModuleName(GetLogTimeModuleEnum.SURPLUS_INVENTORY.name());
        sysBusinessLog.setLogTitle("【编辑】");
        sysBusinessLog.setLogBody(buildEditSurplusInventoryLogBody(surplusInventory, comparisonData));
        sysBusinessLog.setCreateBy(SecurityUtils.getUserId());
        sysBusinessLog.setUpdateBy(SecurityUtils.getUserId());
        this.businessLogService.save(sysBusinessLog);
    }

    /**
     * 上架尾货库存接口的日志
     *
     * @param sysSurplusInventory
     * @param sysSurplusInventorySizeInfos
     */
    @Override
    public void putAwaySurplusInventoryLog(SysSurplusInventory sysSurplusInventory, List<SysSurplusInventorySizeInfo> sysSurplusInventorySizeInfos) {
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        sysBusinessLog.setRefId(sysSurplusInventory.getId());
        sysBusinessLog.setModuleName(GetLogTimeModuleEnum.SURPLUS_INVENTORY.name());
        sysBusinessLog.setLogTitle("【上架】");
        sysBusinessLog.setLogBody(buildPutAwaySurplusInventoryLogBody(sysSurplusInventory, sysSurplusInventorySizeInfos));
        sysBusinessLog.setCreateBy(SecurityUtils.getUserId());
        sysBusinessLog.setUpdateBy(SecurityUtils.getUserId());
        this.businessLogService.save(sysBusinessLog);
    }

    /**
     * 构建上架尾货库存日志体内容
     *
     * @param sysSurplusInventory
     * @param sysSurplusInventorySizeInfos
     * @return
     */
    private String buildPutAwaySurplusInventoryLogBody(SysSurplusInventory sysSurplusInventory, List<SysSurplusInventorySizeInfo> sysSurplusInventorySizeInfos) {
        List<String> bodyLines = new ArrayList<>();
        bodyLines.add(String.format("订单编号：%s", sysSurplusInventory.getProduceOrderId()));
        List<String> beforeSizeInfo = new ArrayList<>();
        List<String> afterSizeInfo = new ArrayList<>();
        for (SysSurplusInventorySizeInfo sysSurplusInventorySizeInfo : sysSurplusInventorySizeInfos) {
            beforeSizeInfo.add(String.format("%s:%s",sysSurplusInventorySizeInfo.getSizeName(),0));
            afterSizeInfo.add(String.format("%s:%s",sysSurplusInventorySizeInfo.getSizeName(),sysSurplusInventorySizeInfo.getInventoryStockQuantity()));
        }
        bodyLines.add(String.format("变动前：%s", String.join("、", beforeSizeInfo)));
        bodyLines.add(String.format("变动后：%s", String.join("、", afterSizeInfo)));
        bodyLines.add(buildOperatorUserInfo());
        return String.join("\r\n", bodyLines);
    }

    /**
     * 构建编辑尾货库存日志体内容
     *
     * @param surplusInventory
     * @param comparisonData
     * @return
     */
    private String buildEditSurplusInventoryLogBody(SysSurplusInventory surplusInventory, Map<String, Tuple> comparisonData) {
        List<String> bodyLines = new ArrayList<>();
        bodyLines.add(String.format("订单编号：%s", surplusInventory.getProduceOrderId()));

        List<String> beforeSizeInfo = new ArrayList<>();
        List<String> afterSizeInfo = new ArrayList<>();

        for (Map.Entry<String, Tuple> entry : comparisonData.entrySet()) {
            beforeSizeInfo.add(String.format("%s:%s",entry.getKey(),entry.getValue().get(0)));
            afterSizeInfo.add(String.format("%s:%s",entry.getKey(),entry.getValue().get(1)));
        }
        bodyLines.add(String.format("变动前：%s", String.join("、", beforeSizeInfo)));
        bodyLines.add(String.format("变动后：%s", String.join("、", afterSizeInfo)));

        bodyLines.add(buildOperatorUserInfo());
        return String.join("\r\n", bodyLines);
    }

    /**
     * 构建新增尾货库存日志体内容
     *
     * @param sysSurplusInventory
     * @param sysSurplusInventorySizeInfos
     * @return
     */
    private String buildAddSurplusInventoryLogBody(SysSurplusInventory sysSurplusInventory, List<SysSurplusInventorySizeInfo> sysSurplusInventorySizeInfos) {
        List<String> bodyLines = new ArrayList<>();
        bodyLines.add(String.format("订单编号：%s", sysSurplusInventory.getProduceOrderId()));

        List<String> beforeSizeInfo = new ArrayList<>();
        List<String> afterSizeInfo = new ArrayList<>();
        for (SysSurplusInventorySizeInfo sysSurplusInventorySizeInfo : sysSurplusInventorySizeInfos) {
            beforeSizeInfo.add(String.format("%s:%s",sysSurplusInventorySizeInfo.getSizeName(),0));
            afterSizeInfo.add(String.format("%s:%s",sysSurplusInventorySizeInfo.getSizeName(),sysSurplusInventorySizeInfo.getInventoryStockQuantity()));
        }
        bodyLines.add(String.format("变动前：%s", String.join("、", beforeSizeInfo)));
        bodyLines.add(String.format("变动后：%s", String.join("、", afterSizeInfo)));

        bodyLines.add(buildOperatorUserInfo());
        return String.join("\r\n", bodyLines);
    }

    /**
     * 构建操作人信息字符串
     * @return
     */
    private String buildOperatorUserInfo(){
        String factoryName = this.factoryService.getFactoryName(SecurityUtils.getFactoryId());
        return String.format("%s-%s", SecurityUtils.getUsername(), factoryName);
    }
}
